package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.dhc.danlu.R;
import cn.com.hd.mall.web.webservices.entity.response.mydl.deliveraddr.DeliverAddressListReponse;
import cn.com.hd.mall.web.webservices.entity.response.mydl.deliveraddr.DeliverAddressListReponse.DeliverAddressListModel;

public class ReceiveAddressBaseListAdapter extends BaseAdapter {
    /**
     * 默认地址为0
     */
    private static final String DEFAULT_ADDRESS_ID = "0";
    private Context context;
    /**
     * 收货地址列表
     */
    private DeliverAddressListReponse addressListReponse;
    private int selectedIndex = 0;

    public ReceiveAddressBaseListAdapter(Context context, DeliverAddressListReponse addressListReponse) {
        this.context = context;
        this.addressListReponse = addressListReponse;
    }

    @Override
    public int getCount() {
        return addressListReponse.getDeliverAddressModelList().size();
    }

    @Override
    public Object getItem(int arg0) {
        return addressListReponse.getDeliverAddressModelList().get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        AddressViewHolder holder;
        if (arg1 == null) {
            holder = new AddressViewHolder();
            arg1 = LayoutInflater.from(context).inflate(R.layout.layout_receive_address_item, null);
            holder.defaultads = (TextView) arg1.findViewById(R.id.defaultads);
            holder.receiverTel = (TextView) arg1.findViewById(R.id.receiverTel);
            holder.receiver = (TextView) arg1.findViewById(R.id.receiver);
            holder.receiveCity = (TextView) arg1.findViewById(R.id.receiveCity);
            holder.imageView = (ImageView) arg1.findViewById(R.id.rAddress_img);
            arg1.setTag(holder);
        } else {
            holder = (AddressViewHolder) arg1.getTag();
        }
        DeliverAddressListModel model = (DeliverAddressListModel) getItem(arg0);
        // 设置数据
        setData(holder, model);
        if (selectedIndex == arg0) {
            // 选中
            holder.imageView.setImageResource(R.drawable.ic_check);
        } else {
            // 未选中
            holder.imageView.setImageResource(R.drawable.ic_not_check);
        }
        return arg1;
    }

    // 更新UI
    private void setData(AddressViewHolder holder, DeliverAddressListModel model) {
        // 省
        String privince = model.getAreaProvinceName();
        // 城市
        String city = model.getAreaCityName();
        // 区
        String district = model.getAreaDistrictName();
        // 地址
        String address = model.getAddressDetail();
        // 设置地址
        holder.receiveCity.setText(privince + city + district + address);
        // 设置姓名
        if (model.getDeliverPerson() != null) {
            holder.receiver.setText(model.getDeliverPerson());
        }
        // 设置电话
        if (model.getDeliverMobile() != null) {
            holder.receiverTel.setText(model.getDeliverMobile());
        }
        // 判断是否默认地址
        if (model.getIsDefault() != null) {
            if (DEFAULT_ADDRESS_ID.equals(model.getIsDefault())) {
                holder.defaultads.setVisibility(View.VISIBLE);
            } else {
                holder.defaultads.setVisibility(View.GONE);
            }
        }
    }

    public void setSelectedIndex(int index) {
        this.selectedIndex = index;
    }

    private static class AddressViewHolder {
        private TextView defaultads;
        private TextView receiverTel;
        private TextView receiver;
        private TextView receiveCity;
        private ImageView imageView;
    }
}